﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;
using Baimasoft.Manage.SqlServerDAL;

public partial class SysManage_OutPage_List : System.Web.UI.Page
{
    UserManage MyUM = new UserManage();
    public DataTable mytb;
    public DataColumn mycolumn;
    public string filedtotal = "";
    public string sHtml = "";
    public string sOrgCode = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        if ((string)Session["Baimasoft_User_LogonID"] == null)
        {
            Response.Redirect("../ReLogon.aspx");
        }

        if (!Page.IsPostBack)
        {
            IniInfo("0");
        }
    }

    public void IniInfo(string sSOrgID)
    {
        mytb = MyUM.GetOutPageInfo();
        foreach (DataColumn mycolumn in mytb.Columns)
        {
            filedtotal = filedtotal + mycolumn.ColumnName + ",";
        }
        sHtml = sHtml + "<script language=\"javascript\" type=\"text/javascript\">";
        filedtotal = filedtotal.Substring(0, filedtotal.Length - 1);
        sHtml = sHtml + " function tree(" + filedtotal + "){";
        string[] sfiledtotal = filedtotal.Split(',');
        int nfiledtotal = sfiledtotal.Length;
        for (int i = 0; i < nfiledtotal; i++)
        {
            sHtml = sHtml + "this." + sfiledtotal[i] + "=" + sfiledtotal[i] + ";";
        }

        if (mytb.Rows.Count > 0)
        {
            int iRowCount = mytb.Rows.Count - 1;
            sHtml = sHtml + "}var trees =new Array(" + iRowCount + ");";
        }
        else
        {
            sHtml = sHtml + "}var trees =new Array(0);";
        }

        for (int j = 0; j < mytb.Rows.Count; j++)
        {
            sHtml = sHtml + "trees[" + j + "] = new tree(";
            string temp = "";
            for (int k = 0; k < mytb.Columns.Count; k++)
            {
                temp = temp + "'" + mytb.Rows[j][k] + "'";
                if (k != mytb.Columns.Count - 1)
                {
                    temp = temp + ",";
                }
            }
            sHtml = sHtml + temp + ");";
        }
        sHtml = sHtml + "GetSuperOrgID('" + sSOrgID + "');show_tree(0,'0');SpreadNewOrgTree();";
        sHtml = sHtml + "</script>";
    }


    protected void BtnDel_Click(object sender, EventArgs e)
    {
        string sOrgID = hOrgID.Value.ToString().Trim();
        string sParentOrgID = hParentOrgID.Value.ToString().Trim();
        string sChildNumber = hChildNumber.Value.ToString().Trim();
        if (sChildNumber != "0")
        {
            IniInfo(sOrgID);
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('该用户组下面有信息,请先删除或者移走后,再删除!');</script>");
            return;
        }
        else if (sOrgID != "")
        {
            int iBack = MyUM.DelOutPageInfo (sOrgID);
            if (iBack > 0)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('删除成功!');</script>");
                IniInfo(sParentOrgID);
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('删除失败!');</script>");
                IniInfo(sOrgID);
            }
        }
        ClearItem();
    }

    protected void BtnRefresh_Click(object sender, EventArgs e)
    {
        string sBack = hOrgID.Value.ToString().Trim();
        IniInfo(sBack);
        ClearItem();
    }
    protected void BtnRefresh2_Click(object sender, EventArgs e)
    {
        string sBack = hParentOrgID.Value.ToString().Trim();
        IniInfo(sBack);
        ClearItem();
    }

    //清空
    public void ClearItem()
    {
        hOrgInfo.Value = "";
        hOrgID.Value = "";
        hParentOrgID.Value = "";
        hChildNumber.Value = "";
    }
    
}
